/**
 * 
 */
package ru.olympus.antonyhands.ao.client.widgets;

import java.util.ArrayList;
import java.util.List;

import ru.olympus.antonyhands.ao.client.AOHelperServiceAsync;
import ru.olympus.antonyhands.ao.shared.item.ItemMarketPriceDTO;
import ru.olympus.antonyhands.ao.shared.util.StringPair;
import ru.olympus.antonyhands.ao.shared.util.StringUtilities;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author AntonyHands
 * @since 01.03.2012
 */
public class ItemHierarchyMarketPriceUpdatePanel extends VerticalPanel {

	private AOHelperServiceAsync _service;
	private ItemsListBox _itemsListBox;

	private Label _errorLabel;
	private FlexTable _table;

	public ItemHierarchyMarketPriceUpdatePanel(AOHelperServiceAsync service,
			ItemsListBox items) {
		_service = service;
		_itemsListBox = items;

		add(new Label("Уточните рыночные цены предметов:"));

		_errorLabel = new Label();
		add(_errorLabel);

		_table = new FlexTable();
		add(_table);
		
		Button refresh = new Button("Обновить");
		add(refresh);
		refresh.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				_errorLabel.setText("");
				List<StringPair> itemPrices = new ArrayList<StringPair>();
				for (int i = 0; i < _table.getRowCount(); i++)
				{
					TextBox tb = (TextBox)_table.getWidget(i, 1);
					/*if (StringUtilities.isEmpty(tb.getText()))
					{
						_errorLabel.setText("Не все рыночные цены указаны!");
						return;
					}*/
					itemPrices.add(new StringPair(tb.getName(), tb.getText()));
				}
				
				_service.updateItemsMarketPrices(itemPrices, new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						_errorLabel.setText(caught.getMessage());
					}

					@Override
					public void onSuccess(String result) {
						// TODO Auto-generated method stub
						if (StringUtilities.isEmpty(result))
						{
							onSuccessfulPricesUpdate();
						}
						else
						{
							_errorLabel.setText(result);
						}
					}
				});
			}
		});
		
		_itemsListBox.addChangeHandler(new ChangeHandler()
        {
            @Override
            public void onChange(ChangeEvent event)
            {
                repaint();
            }
        });
	}

	protected void repaint()
	{
		_errorLabel.setText("");
		_table.removeAllRows();
		_service.listMarketPricesHierarchyList(_itemsListBox.getCurrentValueCode(), 
				new AsyncCallback<List<ItemMarketPriceDTO>>() {
			
			@Override
			public void onSuccess(List<ItemMarketPriceDTO> result) {
				for (int i = 0; i < result.size(); i++)
				{
					_table.setText(i, 0, result.get(i).getItem().getValue());
					TextBox tb = new TextBox();
					tb.setName(result.get(i).getItem().getKey());
					tb.setText(result.get(i).getMarketPrice());
					_table.setWidget(i, 1, tb);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				_errorLabel.setText(caught.getMessage());
			}
		});
	}
	
	/**
	 * Метод вызывается при успешном обновлении рыночных цен
	 */
	public void onSuccessfulPricesUpdate()
	{
		
	}
}
